import React from "react";
import Button from "antd/lib/button";
import Table from "antd/lib/table";
import ConfigProvider from "antd/lib/config-provider";
import zhCN from 'antd/es/locale/zh_CN';
import Project from "@/services/project";
import QueryShowField from "@/services/queryshowfield";
import EmployeeActivityDialog from "@/components/employee/EmployeeActivityDialog";


const getRowClassName = (record, index) => {
  let className = '';
  if (index % 2 === 0) className = "oddRow";
  return className
}

export default class HomeTableList2 extends React.Component {
  state = {
    page: 1,
    pagesize: localStorage.getItem("hometablelist2pagesize") ? (localStorage.getItem("hometablelist2pagesize") !== undefined ? Number(localStorage.getItem("hometablelist2pagesize")) : 20) : 20,
    total: 1,
    order: 1,
    loading: false,
    data: [],
    queryshowfielddata: [],
  };
  componentWillMount() {
    this.fieldsshowload();
    let param = {
      page: this.state.page,
      pagesize: this.state.pagesize,
      code: "",
      name: "",
      major: "",
      area: "",
      certificate: '',
      location: '',
      strcustomername: "",
      representative: "",
      id: 0,
      datadown: false,
    }
    this.seldata(param);
  }
  fieldsshowload = () => {
    QueryShowField.getbycode("customerneedlist1").then(data => {
      this.setState({
        queryshowfielddata: data,
      });
    });
  }
  isEmptyobj = (str) => {
    if (str === '' || str === null || str === 'null' || str === undefined || str === 'undefined') {
      return true;
    } else {
      return false;
    }
  }
  seldata = (params = {}) => {
    params.difference = "1";
    this.setState({ loading: true });
    Project.getlist(params).then(data => {
      this.setState({
        loading: false,
        data: data.data,
        page: params.page,
        pagesize: params.pagesize,
        total: data.total,
        field: params.field,
        order: params.order,
      });
    });
  };

  handleTableChange = (pagination, filters, sorter) => {
    localStorage.setItem("hometablelist2pagesize", pagination.pageSize);
    let field = sorter.field;
    let order = 0;
    if (sorter.order === "descend") {
      order = 1;
    }
    let param = {
      field: field,
      order: order,
      page: pagination.current,
      pagesize: this.state.pagesize,
      code: "",
      name: "",
      major: "",
      area: "",
      certificate: '',
      location: '',
      strcustomername: "",
      representative: "",
      id: 0,
      datadown: false,
    }
    this.seldata(param);
  };
  handleactivityshow = (record) => {
    EmployeeActivityDialog.showModal({ sourceid: record.id, sourcetype: "project",canedit:false })
      .onClose((result = {}) => {
      }).onDismiss(() => {
      });
  };
  onDragEnter = info => {
    window.console.log(info);
  };
  render() {
    const paginationProps = {
      showSizeChanger: true,
      total: this.state.total, // 数据总数
      pageSize: parseInt(this.state.pagesize), // 每页条数
      current: parseInt(this.state.page), // 当前页码
      pageSizeOptions: ["10", "20", "30", "100"],
      showTotal: ((total) => {
        return `共 ${total} 条`;
      }),
    };
    let listcolumns = [];
    if (this.state.queryshowfielddata.fieldsshow !== undefined) {
      (this.state.queryshowfielddata.fieldsshow || []).map(item => {
        let items = this.state.queryshowfielddata.fieldsall.filter((p) => {
          return p.key === item;
        });
        if (items.length > 0) {
          listcolumns.push({
            title: items[0].title,
            dataIndex: item,
            key: item,
            sorter: (a, b) => a[item] > b[item] ? 1 : -1
          })
        }
        return item;
      }
      );
    }
    listcolumns.push({
      title: '操作',
      key: 'action',
      render: (record) => (
        <span>
          <Button size="small" onClick={() => this.handleactivityshow(record)}>
            跟进信息
          </Button>
        </span>
      ),
    });
    return (
      <ConfigProvider locale={zhCN}>
        <div>
          <Table rowKey='num'
            rowClassName={(record, index) => getRowClassName(record, index)}
            columns={listcolumns}
            scroll={{ x: 'max-content' }}
            dataSource={this.state.data}
            pagination={paginationProps}
            loading={this.state.loading}
            onChange={this.handleTableChange}
          />
        </div>
      </ConfigProvider>
    );
  }
}